﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Classes.*;
    import Engine.Events.*;
    import flash.display.*;
    import Classes.*;

    public class AnimationEffect extends MapResourceEffect {

        protected var m_effectName:String;
        protected var m_itemImage:ItemImage;
        protected var m_effectImage:DisplayObject;
        protected var m_lastFrame:int;
        protected var m_isActive:Boolean;
        protected var m_isAutoUpdating:Boolean;
        protected var m_isLooping:Boolean;
        protected var m_originalFPS:Number;
        protected var m_isCentering:Boolean;

        public function AnimationEffect(_arg1:MapResource, _arg2:String, _arg3:Boolean=false, _arg4:Boolean=false, _arg5:Boolean=false){
            this.m_effectName = _arg2;
            this.m_isAutoUpdating = _arg3;
            this.m_isLooping = _arg4;
            this.m_lastFrame = -1;
            this.m_isActive = true;
            this.m_isCentering = _arg5;
            super(_arg1, 60);
        }
        public function get isAutoUpdating():Boolean{
            return (this.m_isAutoUpdating);
        }
        public function get isLooping():Boolean{
            return (this.m_isLooping);
        }
        override public function animate(_arg1:uint):Boolean{
            var _local2:AnimatedBitmap;
            var _local3:DisplayObject;
            var _local4:Number;
            var _local5:Boolean;
            if (!this.m_isActive){
                return (this.m_isActive);
            };
            if (!this.m_itemImage){
                this.loadAnimationEffect();
            };
            if (this.m_effectImage){
                _local2 = (this.m_effectImage as AnimatedBitmap);
                _local3 = (m_mapResource.content as DisplayObject);
                if (_local2){
                    if (this.m_isAutoUpdating){
                        _local4 = (1 / this.m_originalFPS);
                        this.updateAnimatedBitmap(_local4);
                    };
                    if (!_local2.isPlaying){
                        _local2.reset();
                    };
                    if (!this.m_isLooping){
                        _local5 = (((_local2.currentFrame < this.m_lastFrame)) || ((_local2.currentFrame == (_local2.numFrames - 1))));
                        if (((_local2.isPlaying) && (_local5))){
                            _local2.stop();
                            m_mapResource.removeAnimatedEffects();
                            _local3.visible = true;
                            this.m_isActive = false;
                            this.onAnimationEnded();
                        } else {
                            this.m_lastFrame = _local2.currentFrame;
                        };
                    };
                } else {
                    this.m_isActive = false;
                };
            };
            return (this.m_isActive);
        }
        protected function onAnimationEnded():void{
        }
        public function updateAnimatedBitmap(_arg1:Number):void{
            var _local2:AnimatedBitmap;
            if (this.m_effectImage){
                _local2 = (this.m_effectImage as AnimatedBitmap);
                if (_local2){
                    _local2.onUpdate(_arg1);
                } else {
                    this.m_isActive = false;
                };
            };
        }
        override public function reattach():void{
            if (((!(this.m_itemImage)) || (!(this.m_itemImage.getInstance())))){
                return;
            };
            if (!this.m_effectImage){
                this.m_effectImage = (this.m_itemImage.getInstance().image as DisplayObject);
            };
            if (this.m_isCentering){
                this.m_effectImage.x = ((m_mapResource.content.x + (m_mapResource.content.width >> 1)) - (this.m_effectImage.width >> 1));
                this.m_effectImage.y = ((m_mapResource.content.y + (m_mapResource.content.height >> 1)) - (this.m_effectImage.height >> 1));
            } else {
                this.m_effectImage.x = ((-((this.m_effectImage.width >> 1)) + this.m_itemImage.offsetX) + x);
                this.m_effectImage.y = ((-((this.m_effectImage.height >> 1)) + this.m_itemImage.offsetY) + y);
            };
            var _local1:Sprite = this.getAttachParent();
            if (_local1){
                _local1.addChild(this.m_effectImage);
            };
            var _local2:AnimatedBitmap = (this.m_effectImage as AnimatedBitmap);
            if (!_local2){
                this.m_isActive = false;
            };
        }
        protected function getAttachParent():Sprite{
            var _local1:Sprite = (m_mapResource.getDisplayObject() as Sprite);
            return (_local1);
        }
        override public function cleanUp():void{
            if (((this.m_effectImage) && (this.m_effectImage.parent))){
                this.m_effectImage.parent.removeChild(this.m_effectImage);
                this.m_effectImage = null;
            };
            if (this.m_itemImage){
                this.m_itemImage.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                this.m_itemImage = null;
            };
            this.m_isActive = false;
            m_isCompleted = true;
        }
        protected function loadAnimationEffect():void{
            var _local2:XML;
            var _local3:XML;
            if (((!((m_mapResource == null))) && ((m_mapResource.getItem() == null)))){
                this.m_isActive = false;
                return;
            };
            var _local1:Boolean = ((((m_mapResource.content) && (m_mapResource.getItemImage()))) && (!(m_mapResource.isCurrentImageLoading())));
            if (((_local1) && (!(this.m_itemImage)))){
                _local2 = Global.gameSettings.getEffectByName(this.m_effectName);
                _local3 = _local2.image[0];
                this.m_originalFPS = ((_local3.@fps) || (1));
                this.m_itemImage = new ItemImage(_local3);
                this.m_itemImage.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                this.m_itemImage.load();
            };
        }
        protected function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImageInstance;
            if (_arg1.target == this.m_itemImage){
                _local2 = this.m_itemImage.getInstance();
                if (!_local2){
                    return;
                };
                this.m_itemImage.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                this.reattach();
            };
        }
        public function isComplete():Boolean{
            return (!(this.m_isActive));
        }
        public function get effectImage():DisplayObject{
            return (this.m_effectImage);
        }
        public function set effectImage(_arg1:DisplayObject):void{
            this.m_effectImage = _arg1;
        }

    }
}//package Classes.effects 
